using System.Collections.Generic;
using System.Linq;
using TeachingManagementSystem.DAL;
using TeachingManagementSystem.Models;

namespace TeachingManagementSystem.BLL
{
    public class CourseBLL
    {
        private DataContext _context;

        public CourseBLL()
        {
            _context = new DataContext();
        }

        // 获取所有课程
        public List<Course> GetAllCourses()
        {
            return _context.Courses?.ToList() ?? new List<Course>();
        }

        // 根据ID获取课程
        public Course GetCourseByID(int courseID)
        {
            if (courseID <= 0)
                return null;
            return _context.Courses?.FirstOrDefault(c => c.CourseID == courseID);
        }

        // 根据教师ID获取课程
        public List<Course> GetCoursesByTeacherID(int teacherID)
        {
            if (teacherID <= 0)
                return new List<Course>();
            return _context.Courses?.Where(c => c.TeacherID == teacherID).ToList() ?? new List<Course>();
        }

        // 添加课程
        public void AddCourse(Course course)
        {
            if (course == null)
                return;
            
            _context.Courses.Add(course);
            _context.SaveChanges();
        }

        // 更新课程
        public void UpdateCourse(Course course)
        {
            if (course == null || course.CourseID <= 0)
                return;
                
            var existingCourse = _context.Courses?.FirstOrDefault(c => c.CourseID == course.CourseID);
            if (existingCourse != null)
            {
                existingCourse.CourseCode = course.CourseCode;
                existingCourse.CourseName = course.CourseName;
                existingCourse.Description = course.Description;
                existingCourse.Credit = course.Credit;
                existingCourse.TotalHours = course.TotalHours;
                existingCourse.Department = course.Department;
                existingCourse.TeacherID = course.TeacherID;
                existingCourse.Status = course.Status;
                
                _context.SaveChanges();
            }
        }

        // 删除课程
        public void DeleteCourse(int courseID)
        {
            if (courseID <= 0)
                return;
                
            var courses = _context.Courses;
            if (courses != null)
            {
                var course = courses.FirstOrDefault(c => c.CourseID == courseID);
                if (course != null)
                {
                    courses.Remove(course);
                    _context.SaveChanges();
                }
            }
        }

        // 获取所有唯一的系部列表
        public List<string> GetAllDepartments()
        {
            return _context.Courses?.Where(c => !string.IsNullOrEmpty(c.Department))
                .Select(c => c.Department)
                .Distinct()
                .OrderBy(d => d)
                .ToList() ?? new List<string>();
        }

        // 根据系部获取课程
        public List<Course> GetCoursesByDepartment(string department)
        {
            if (string.IsNullOrEmpty(department))
                return new List<Course>();
                
            return _context.Courses?.Where(c => c.Department == department).ToList() ?? new List<Course>();
        }
    }
}